﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using cart.model;
using cart.utils;

namespace cart.dao
{
    public class orderItemsDao
    {
        public ArrayList getOrderItemsByOrderID(int orderID)
        {
            string SELECT_ORDERITEMS_BY_USERNAME = "select * from orderItems where orderID=@orderID";
            SqlConnection conn = DB.getSqlConnection();
            ArrayList alOrderItmes = new ArrayList();
            DB.connectionOpen(conn);

            SqlCommand cmd = new SqlCommand(SELECT_ORDERITEMS_BY_USERNAME,conn);
            cmd.Parameters.AddWithValue("@orderID", orderID);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                orderItemsModel oim = new orderItemsModel();
                oim.orderID = sdr.GetInt32(0);
                oim.productID = sdr.GetInt32(1);
                oim.prodcutName = sdr.GetString(2);
                oim.score = sdr.GetInt32(3);
                oim.marketPrice = Convert.ToDouble(sdr.GetValue(4));
                oim.memberPrice = Convert.ToDouble(sdr.GetValue(5));
                oim.productNumber = sdr.GetInt32(6);
                alOrderItmes.Add(oim);
            }
            DB.connectionClose(conn);
            return alOrderItmes;
        }

        public Boolean addOrderItem(orderItemsModel oim)
        {
            string INSERT_INTO_ORDERITEMS = "INSERT INTO orderItems  VALUES(@orderID,@productID ,@productName,@score,@marketPrice,@memberPrice,@productNumber)";
            SqlParameter[] parms = new SqlParameter[] 
            {
                new SqlParameter("@orderID",oim.orderID),
                new SqlParameter("@productID",oim.productID),
                new SqlParameter("@productName",oim.prodcutName),
                new SqlParameter("@score",oim.score),
                new SqlParameter("@marketPrice",oim.marketPrice),
                new SqlParameter("@memberPrice",oim.memberPrice),
                new SqlParameter("@productNumber",oim.productNumber)
            };
            return DB.executeNonQuerySQL(INSERT_INTO_ORDERITEMS, parms);
        }
    }
}
